package com.example.springbootrabbitmqconsumer.config;



import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Ah Nan
 * @Description: rabbitMQ 启动配置
 * @Date: 15:40 2020/6/24
 */
@Configuration
public class RabbitConfig {

    public static final String firstQueue = "FIRST_QUEUE";


    public static final String secondQueue = "SECOND_QUEUE";


    public static final String thirdQueue = "THIRD_QUEUE";


    public static final String fourthQueue = "FOURTH_QUEUE";

    //直连交换机
    public static final String directExchange = "DIRECT_EXCHANGE";

    //topic交换机
    public static final String topicExchange = "TOPIC_EXCHANGE";

    //队列交换机
    public static final String fanoutExchange = "FANOUT_EXCHANGE";

    //直连类型交换机路由关键字
    public static final String DIRECT_EXCHANGE_ROUTING_KEY = "rabbitmq.best";

    //topic类型交换机路由关键字
    public static final String TOPIC_EXCHANGE_ROUTING_KEY = "*.rabbitmq.*";

    //创建四个队列
    @Bean
    public Queue getFirstQueue() {
        return new Queue(firstQueue);
    }

    @Bean
    public Queue getSecondQueue() {
        return new Queue(secondQueue);
    }

    @Bean
    public Queue getThirdQueue() {
        return new Queue(thirdQueue);
    }

    @Bean
    public Queue getFourthQueue() {
        return new Queue(fourthQueue);
    }

    //创建三个交换机
    @Bean
    public DirectExchange getDirectExchange() {
        return new DirectExchange(directExchange);
    }

    @Bean
    public TopicExchange getTopicExchange() {
        return new TopicExchange(topicExchange);
    }

    @Bean
    public FanoutExchange getFanoutExchange() {
        return new FanoutExchange(fanoutExchange);
    }

    //定义四个绑定关系
    @Bean
    public Binding bindFirst() {
        return BindingBuilder.bind(getFirstQueue()).to(getDirectExchange()).with(DIRECT_EXCHANGE_ROUTING_KEY);
    }

    @Bean
    public Binding bindSecond() {
        return BindingBuilder.bind(getSecondQueue()).to(getTopicExchange()).with(TOPIC_EXCHANGE_ROUTING_KEY);
    }

    @Bean
    public Binding bindThird() {
        return BindingBuilder.bind(getThirdQueue()).to(getFanoutExchange());
    }

    @Bean
    public Binding bindFourth() {
        return BindingBuilder.bind(getFourthQueue()).to(getFanoutExchange());
    }

}

